package com.example.sherlockzhong.plush_v2.Models;

import com.example.sherlockzhong.plush_v2.Constant;
import com.example.sherlockzhong.plush_v2.Constants.UrlConstant;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;
import com.example.sherlockzhong.plush_v2.Utils.StorageUtils;

import org.json.JSONArray;
import org.json.JSONTokener;

/*
 * Created by SherlockZhong on 9/11/14.
 */

public class ExamsModel {

    public static String getExams() {
        if (Constant.STUDENT_ID != null && Constant.STUDENT_TOKEN != null) {
            try {
                return NetworkUtils.httpGet(String.format(UrlConstant.examsUrl, Constant.STUDENT_ID, Constant.STUDENT_TOKEN));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static boolean saveExams(String grades) {
        return StorageUtils.saveAsFile("Exams.json", grades);
    }

    public static JSONArray retrieveExams() {
        try {
            String raw = StorageUtils.retrieveFromFile("Exams.json");
            JSONTokener jsonTokener = new JSONTokener(raw);
            return (JSONArray) jsonTokener.nextValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
